package com.michaelbulava.TFS.tasks;

import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.project.Project;
import com.michaelbulava.TFS.Messages;
import com.microsoft.tfs.core.TFSTeamProjectCollection;
import com.microsoft.tfs.core.clients.commonstructure.CommonStructureClient;
import com.microsoft.tfs.core.clients.commonstructure.ProjectInfo;
import com.microsoft.tfs.util.Check;

/**
 * Created by mbulava on 6/19/2015.
 */
public class QueryTeamProjectsTask extends BackgroundableTask{
    private final TFSTeamProjectCollection connection;
    private ProjectInfo[] projects = null;

    public QueryTeamProjectsTask(Project project, TFSTeamProjectCollection connection) {
        super(project, Messages.getString("QueryTeamProjectsCommand.CommandText"), true);
        Check.notNull(connection, "connection");
        this.connection = connection;
    }

    @Override
    protected boolean runInBackground() {
        return false;
    }

    @Override
    public void run(ProgressIndicator progressIndicator) {
        progressIndicator.setIndeterminate(true);
        try{
            CommonStructureClient cssClient = this.connection.getCommonStructureClient();
            this.projects = cssClient.listProjects();
        } finally {
            progressIndicator.stop();
        }
    }

    public ProjectInfo[] getProjects(){
        return this.projects;
    }
}
